package com.i2works.smartluolong.manage.controller.common;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.manage.entity.admin.AdminRoleDO;
import com.i2works.smartluolong.manage.entity.admin.AdminRoleQuery;
import com.i2works.smartluolong.manage.service.admin.IAdminRoleService;
import com.i2works.smartluolong.manage.utils.BaseModule;
import com.i2works.smartluolong.utils.base.CompanyConstant;
import com.i2works.smartluolong.utils.base.GlobalConstant;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.flow.*;
import com.i2works.smartluolong.web.service.flow.IFlowProcessService;
import com.i2works.smartluolong.web.service.flow.IFlowService;
import com.i2works.smartluolong.web.service.flow.IFlowTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 工作流公共控制层
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/5/4 11:11
 */

@Component
public class CommonItemController extends BaseModule {

    public final IAdminRoleService adminRoleService;

    public final IFlowTemplateService flowTemplateService;

    public final IFlowService flowService;

    public final IFlowProcessService flowProcessService;

    /**
     * 系统预留角色key；如果某个角色的key是以下几个，那么这个角色将会查看所有的事项，不受角色限制；
     */
    @Value("${manage-reserved-rolekey}")
    private String manageReservedRolekey;

    /**
     * 线程池
     */
    protected ExecutorService executorService = Executors.newCachedThreadPool();


    @Autowired
    public CommonItemController(IAdminRoleService adminRoleService, IFlowTemplateService flowTemplateService, IFlowService flowService, IFlowProcessService flowProcessService) {
        this.adminRoleService = adminRoleService;
        this.flowTemplateService = flowTemplateService;
        this.flowService = flowService;
        this.flowProcessService = flowProcessService;
    }

    /**
     * 查询流程进度
     *
     * @param id       业务编号
     * @param itemType 业务类型
     */
    public List<FlowProcessDO> queryFlowProcess(Integer id, String itemType) throws ServiceException {
        //查询流程
        FlowProcessQuery flowProcessQuery = new FlowProcessQuery();
        flowProcessQuery.setItemId(id);
        flowProcessQuery.setItemType(itemType);
        Integer queryCount = flowProcessService.queryCount(flowProcessQuery);
        if (queryCount > 0) {
            return flowProcessService.queryList(flowProcessQuery);
        }
        return null;
    }


    /**
     * 获取当前用户的角色ID
     */
    public AdminRoleDO getAdminRole() throws ServiceException {
        Integer userId = getUserId();
        AdminRoleQuery roleQuery = new AdminRoleQuery();
        roleQuery.setUserId(userId);
        roleQuery.setStatus(GlobalConstant.status.TRUE);
        return adminRoleService.queryUserRole(roleQuery);
    }


    /**
     * 获取工作流模板
     */
    public FlowTemplateDO getFlowTemplate(Integer adminRoleId, String itemCode) throws ServiceException {
        //获取工作流
        FlowQuery flowQuery = new FlowQuery();
        flowQuery.setItemCode(itemCode);
        flowQuery.setStatus(GlobalConstant.status.TRUE);
        FlowDO flowDO = flowService.query(flowQuery);
        Integer flowId = flowDO.getId();
        //获取工作流模板
        FlowTemplateQuery templateQuery = new FlowTemplateQuery();
        templateQuery.setFlowId(flowDO.getId());
        AdminRoleQuery adminRoleQuery = new AdminRoleQuery();
        adminRoleQuery.setId(adminRoleId);
        AdminRoleDO adminRoleDO = adminRoleService.query(adminRoleQuery);
        //是否保留角色标识.保留标识可无视任何角色显示，查看事项列表
        if (!existReserved(adminRoleDO)) {
            templateQuery.setRoleId(adminRoleId);
        }
        return flowTemplateService.query(templateQuery);
    }

    /**
     * 标示是否存在
     */
    public static boolean actionContains(String action) {
        return Lists.newArrayList(CompanyConstant.action.HANDLE, CompanyConstant.action.INDEX, CompanyConstant.action.INVALID, CompanyConstant.action.PASS, CompanyConstant.action.WAIT, CompanyConstant.action.REFUSE).contains(action);
    }


    /**
     * 是否保留角色标识
     * @param adminRoleDO
     * @return true是，false否
     */
    public Boolean existReserved(AdminRoleDO adminRoleDO) {
        if (StringUtils.isNotBlank(manageReservedRolekey)) {
            List<String> list = Lists.newArrayList(manageReservedRolekey.split(","));
            return adminRoleDO.getId() == 1 || list.contains(adminRoleDO.getRoleKey());
        }
        return false;
    }


}
